<?php

namespace app\admin\controller;

use core\Request;
use think\facade\Db;

class Ajax extends Base
{
    /**
     * 更新字段 主库
     */
    public function upField()
    {
        $request = Request::instance();
        $method = $request->method();
        if ($method == 'POST') {
            $table = $request->post('table', '');                //表名
            $id_name = $request->post('id_name', '');            //条件字段
            $id_value = $request->post('id_value', '');          //条件值
            $field = $request->post('field', '');                //修改的字段
            $field_value = $request->post('field_value', '');    //修改的值
        } else {
            $table = $request->get('table', '');                //表名
            $id_name = $request->get('id_name', '');            //条件字段
            $id_value = $request->get('id_value', '');          //条件值
            $field = $request->get('field', '');                //修改的字段
            $field_value = $request->get('field_value', '');    //修改的值
        }

        if ($table == '' || $id_name == '' || $id_value == '' || $field == '' || $field_value === '') {
            jsonApi(0, '参数不足' . $table);
        }

        $table = strtolower($table);
        $where[$id_name] = $id_value;
        $status = Db::name($table)->where($where)->update([$field => $field_value]);
        if ($status) {
            jsonApi(1, '操作成功');
        } else {
            jsonApi(0, '操作失败');
        }
    }

}
